import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,parseToChart} from '../apis/core-util'
import basicInfo from '../apis/basicInfo'
var viewTypeParam = {
  domain:[function(){
    return {
      classify:'domain',
      statusCode:'400',
      domainId:this.domainId
    }
  },function(){
    return {
      classify:'domain',
      errorCode:'0',
      domainId:this.domainId
    }
  }],
  statuscode:[function(){
    return {
      classify:'statuscode',
      statusCode:this.statuscode,
      codeEq:true
    }
  },function(){
    return {
      classify:'domain',
      statusCode:this.statuscode,
      codeEq:true
    }
  }],
  errorcode:[function(){
    return {
      classify:'errorcode',
      errorCode:this.errorcode,
      codeEq:true
    }
  },function(){
    return {
      classify:'domain',
      errorCode:this.errorcode,
      codeEq:true
    }
  }]
}

export default class MainScope extends Scope {
  constructor(props){
    super();
    this.domainId = props.routeParams.domainId
    this.statuscode = props.routeParams.statuscode
    this.errorcode = props.routeParams.errorcode

    if(this.domainId){
      this.viewType = 'domain'
    }else if(this.statuscode){
      this.viewType = 'statuscode'
    }else if(this.errorcode){
      this.viewType = 'errorcode'
    }
    this.$state = {
      title:'',
      chart0:{
        loading:true
      },
      chart1:{
        loading:true
      },
      grid:{
        loading:true
      }
    }
    this.chart0Api = coreApi({
      limit:5,
      viewKey:'error_rate',
      urlName:'errorrate'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    },viewTypeParam[this.viewType][0].bind(this));
    this.chart1Api = coreApi({
      limit:5,
      viewKey:'error_rate',
      urlName:'errorrate'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    },viewTypeParam[this.viewType][1].bind(this));
    this.gridApi = coreApi({
      urlName:'httperrorsimilar',
      size:1,
      limit:200,
      order:'101'
    },TimeStore.getValue,viewTypeParam[this.viewType][1].bind(this));
  }
  fetch(){
    var _this = this
    this.chart0Api.fetch().then(function(data,params){
      _this.$state.chart0 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart0 = {
        error:err
      }
    },function(params){
      _this.$state.chart0 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart1Api.fetch().then(function(data,params){
      _this.$state.chart1 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart1 = {
        error:err
      }
    },function(params){
      _this.$state.chart1 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.fetchGrid()
  }
  fetchGrid(){
    var _this = this
    this.gridApi.fetch().then(function gridHandle(data,params){
      _this.$state.grid = {
        data:_.map(data.result,function(errorType){
          return _.extend({},errorType.key,errorType.combData)
        })
      }
    },function(err){
      _this.$state.grid = {
        error:err
      }
    },function(params){
      _this.$state.grid = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
  }
  $settime(payload){
    this.fetch()
  }
  $unload(){
  }
  $load(){
    var _this = this
    if(this.viewType==='domain'){
      basicInfo({
        id:this.domainId,
        urlName:'url'
      }).fetch().done(function(data){
        _this.$state.title = data.result[0].name;
        _this.emit()
      })
    }else if(this.viewType==='statuscode'){
      _this.$state.title = this.statuscode;
      _this.emit()
    }else if(this.viewType==='errorcode'){
      basicInfo({
        id:this.errorcode,
        urlName:'networkerror'
      }).fetch().done(function(data){
        _this.$state.title = data.result[0].name;
        _this.emit()
      })
    }
    this.fetch();
  }
}
